﻿using System;
using System.Threading.Tasks;

namespace ESRI.ArcGIS.Client.Async
{
    public static partial class ILegendSupportExtensions
    {
        /// <summary>
        /// Queries for the legend infos of the layer.
        /// </summary>
        /// <param name="legendSupport"></param>
        /// <returns></returns>
        public static Task<LayerLegendInfo> QueryLegendInfosTaskAsync(this ILegendSupport legendSupport)
        {
            if (legendSupport == null) throw new ArgumentNullException("legendSupport");

            var tcs = new TaskCompletionSource<LayerLegendInfo>();
            legendSupport.QueryLegendInfos(tcs.SetResult,
                exception =>
                {
                    if (exception == null)
                        exception = new ArgumentNullException("exception", "Check if the layer was initialized");
                    tcs.SetException(exception);
                });
            return tcs.Task;
        }

    }
}
